$(function () {
    $(".mainbtn").click(function () {
        $(".main").css({ display: "none" })
        $(".box").css({ display: "block" })
    })


    var span1 = document.getElementsByClassName("l1")[0];
    var span2 = document.getElementsByClassName("l2")[0]
    var span3 = document.getElementsByClassName("l3")[0]
    var span4 = document.getElementsByClassName("l4")[0]
    var qiangdu = document.getElementsByClassName("qr")[0]

    var arr = [];
    for (i = 48; i <= 57; i++) {
        var cahr = String.fromCharCode(i);//根据编码返回对应的字符
        arr.push(cahr);
    }
    // 大写字母
    var arr1 = [];
    for (i = 65; i <= 90; i++) {
        var cahr = String.fromCharCode(i);//根据编码返回对应的字符
        arr1.push(cahr);
    }
    // 小写字母
    var arr2 = [];
    for (i = 97; i <= 122; i++) {
        var cahr = String.fromCharCode(i);//根据编码返回对应的字符
        arr2.push(cahr);
    }




    $(".user").change(judgeuser);
    $(".pwd").change(judegpwd);
    $(".phone").change(judegphone);
    $(".emil").change(judgeemail);
    $(".zhuce").click(registerhander)



    async function judgeuser() {
        var uservalue = $(".user").val();
        var reg1 = /^.{6,12}$/;
        var reg2 = /^[0-9]/;
        var reg3 = /[^\w]/;  //非正常元素
        if (!reg1.test(uservalue)) {
            span1.innerText = " 用户名要求  6-12位 ";
            span1.style.color = "red"
            span1.style.fontSize = "12px"
        }
        else if (reg2.test(uservalue)) {
            span1.innerText = " 用户名要求不能以数字开头 "
            span1.style.color = "red"
            span1.style.fontSize = "12px"
        }
        else if (reg3.test(uservalue)) {
            span1.innerText = " 用户名要求大小写字母, _   组成 "
            span1.style.color = "red"
            span1.style.fontSize = "12px"
        }
        else {
            var result = await isexituser({ user: uservalue });
            var { status, msg } = result;
            if (status) {
                span1.innerText = "用户名格式正确"
                span1.style.color = "blue"
                span1.style.fontSize = "12px"
                return true
            }
            else {
                span1.innerText = msg
                span1.style.color = "red"
                span1.style.fontSize = "12px"
            }
        }
        // return false;
        return false;

    }

    async function judegpwd() {
        // debugger;
        var pwdvalue = $(".pwd").val();
        var reg = /^[0-9A-Za-z]{6,12}$/;
        if (reg.test(pwdvalue)) {
            span2.innerText = "密码输入格式正确"
            span2.style.color = "blue"
            span2.style.fontSize = "12px"

            var isreg1 = /[0-9]/.test(pwdvalue);
            var isreg2 = /[a-z]/.test(pwdvalue);
            var isreg3 = /[A-Z]/.test(pwdvalue);
            var count = isreg1 + isreg2 + isreg3;
            if (count == 1) {
                qiangdu.innerText = `密码强度为：弱`
            } else if (count == 2) {
                qiangdu.innerText = "密码强度为：中"
            }
            else if (count == 3) {
                qiangdu.innerText = "密码强度为：强"
            }
            return true

        }
        else {
            span2.innerText = " 密码要求    6-12位  由数字,大小写字母组成"
            span2.style.color = "red"
            span2.style.fontSize = "12px"
        }
        return false;
    }

    async function judegphone() {
        var phonevalue = $(".phone").val();
        var reg = /^1(3|5|6|7|8|9)\d{9}$/;
        if (reg.test(phonevalue)) {
            var result = await isexitphone({ phone: phonevalue });
            var { status, msg } = result;
            if (status) {
                span3.innerText = "手机号输入正确"
                span3.style.color = "blue"
                span3.style.fontSize = "12px"
                return true;
            }
            else {
                span3.innerText = msg
                span3.style.color = "red"
                span3.style.fontSize = "12px"
            }
        }
        else {
            span3.innerText = " 请输入正确的手机号"
            span3.style.color = "red"
            span3.style.fontSize = "12px"
        }
        return false;

    }

    async function judgeemail() {
        var emilvalue = $(".emil").val();
        var reg = /^\w{6,12}@\w{2,8}\.(com|cn|net)$/;
        if (reg.test(emilvalue)) {
            var result = await isexitemail({ email: emilvalue })//发生请求=>得到promise实例(进行中)
            var { status, msg } = result;
            if (status) {
                span4.innerText = "邮箱输入正确"
                span4.style.color = "blue"
                span4.style.fontSize = "12px"
                return true;
            }
            else {
                span4.innerText = msg
                span4.style.color = "red"
                span4.style.fontSize = "12px"
            }
        }
        else {
            span4.innerText = " 请输入正确的邮箱格式"
            span4.style.color = "red"
            span4.style.fontSize = "12px"
        }
        return false
    }


    async function registerhander() {
        // 按顺序验证=>全都成功再验证
        // var isa1 = await judgeuser();
        // var isa2 = await judegpwd();
        // var isa3 = await judegphone();
        // var isa4 = await judgeemail();

        var list = await Promise.all([judgeuser(), judegpwd(), judegphone(), judgeemail()]);//新的promise实例
        console.log(list);
        var flag = list.every(item => item === true);
        console.log(flag);
        if (flag) {
            var user1 = $(".user").val();
            var pwd1 = $(".pwd").val();
            var phone1 = $(".phone").val();
            var email1 = $(".emil").val();
            console.log(user1, pwd1, phone1, email1);
            var result = await registerjie({ user: user1, pwd: pwd1, email: email1, phone: phone1 })
            console.log(result);
            var { status, msg } = result;
            console.log(status);
            if (status) {
                location.href = "login_account.html"
            } else {
                console.log(msg);
            }
        }
    }
    $(".eye1").click(function () {
        console.log(this);
        $(".pwd").prop("type", "text")
        $(".eye1").css({ display: "none" })
        $(".eye2").css({ display: "inline-block" })
    })

    $(".eye2").click(function () {
        $(".pwd").prop("type", "password")
        $(".eye2").css({ display: "none" })
        $(".eye1").css({ display: "inline-block" })
    })


})